import 'package:flutter/material.dart';

class MSAnimatedSwitcherDemo1 extends StatefulWidget {
  const MSAnimatedSwitcherDemo1({Key? key}) : super(key: key);

  @override
  State<MSAnimatedSwitcherDemo1> createState() =>
      _MSAnimatedSwitcherDemo1State();
}

class _MSAnimatedSwitcherDemo1State extends State<MSAnimatedSwitcherDemo1> {
  var _count = 0;

  List<Widget> list = [
    Container(
      width: 100,
      height: 100,
      color: Colors.green,
      key: ValueKey(Container(
        width: 100,
        height: 100,
        color: Colors.orange,
      )),
    ),
    Container(
        width: 50,
        height: 50,
        color: Colors.orange,
        key: ValueKey(Container(
          width: 100,
          height: 100,
          color: Colors.green,
        ))),
  ];

  @override
  Widget build(BuildContext context) {
    final _text = _count < 10 ? "0$_count" : "$_count";

    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        child: AnimatedSwitcher(
          // child: Text(
          //   _text,
          //   textScaleFactor: 1.5,
          //   key: ValueKey(_text),
          // ),
          // child: list[_count],
          child: InkWell(
            key: ValueKey(_count),
              child:  _count == 0 ? Container(
                  width: 100,
                  height: 100,
                  color: Colors.green,
                 ):Container(width: 100,height: 100,color: Colors.orange,)),
          transitionBuilder: (child, anim) {
            var tween = Tween<Offset>(begin: Offset(1, 0), end: Offset(0, 0));
            return SlideTransition(
              position: tween.animate(anim),
              child: child,
            );
          },
          duration: Duration(milliseconds: 500),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          if (_count == 1) {
            setState(() {
              _count = 0;
            });
          } else {
            setState(() {
              _count = 1;
            });
          }
        },
      ),
    );
  }
}
